class Solution {
public:
    int numberOfArrays(vector<int>& differences, int lower, int upper) {
        int lo = 0, hi = 0, sum = 0;
        for (int d : differences) {
            sum += d, lo = min(lo, sum), hi = max(hi, sum);
            if (hi - lo > upper - lower) return 0;
        }
        return (upper - lower) - (hi - lo) + 1;
    }
};
